// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
//
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================
#include "./TcObjectRegistry.h"
#include "./TcObject.h"
#include "./TcView.h"
#include <stddef.h>
#include <string>

using namespace std;

//=============================================================================
TcObjectRegistry::TcObjectRegistry() :
    maxid_(0),
    lock_(0)
{
}

//=============================================================================
TcObjectRegistry& TcObjectRegistry::registry(void)
{   static TcObjectRegistry* registry = NULL;
    if ( registry == NULL )
    {   registry = new TcObjectRegistry();
    }
    return *registry;
}

//=============================================================================
TcObject* TcObjectRegistry::find(int id)
{   std::map<int,TcObject*>::iterator it = objects_.find(id);
    if ( it == objects_.end() ) return NULL;
    return it->second;
}

//=============================================================================
TcObject* TcObjectRegistry::find(const char* name)
{   std::string NAME(name);
    std::map<string,TcObject*>::iterator it = named_.find(NAME);
    if ( it == named_.end() ) return NULL;
    return it->second;
}



//=============================================================================
void TcObjectRegistry::lockDecrement_()
{   lock_--;
    if ( lock_ > 0) return;

    std::list<TcView*>::iterator itView;

    std::list<TcObject*>::iterator itObj;
    for ( itObj = outdated_.begin(); itObj != outdated_.end(); itObj++ )
    {   if ( (*itObj)->status_ == TcObject::OS_NEW )
        {   for ( itView = views_.begin(); itView != views_.end(); itView++ )
            {   (*itView)->append(*itObj);
            }
        }

        (*itObj)->status_= TcObject::OS_UPTODATE;
    }

    for ( itView = views_.begin(); itView != views_.end(); itView++ )
    {   (*itView)->refresh();
    }
}

//=============================================================================
